<script type="text/javascript">
  RED.nodes.registerType('ads-client-connection-status', {
    paletteLabel: 'ADS - Connection Status',
    category: 'TwinCAT ADS',
    icon: 'font-awesome/fa-info',
    align: 'right',
    color: '#3FADB5',
    inputLabels: 'Trigger',
    outputs: 1,
    outputLabels: 'Status',
    defaults: { 
      name: { value: '' },
      connection: { value: null, type: "ads-client-connection" },
      showInput: { value: false },
      inputs: { value: 0 }
    },
    label: function () {
      if (this.connection === null)
        return `${(this.name || `ADS - Connection Status`)} (*Not configured*)`;

      if (this.name) {
        return this.name
      }

      const connectionNode = RED.nodes.node(this.connection)

      if (connectionNode && connectionNode.name)
        return `ADS - Connection Status (${connectionNode.name})`;
      else if (connectionNode)
        return `ADS - Connection Status (${connectionNode.targetAmsNetId}:${connectionNode.targetAdsPort})`;
      
      return `ADS - Connection Status`;
    },
    oneditprepare: function () {
      //Stuff that is done when properties panel is opened
      let node = this

      let checkIfInput = function () {
        node.inputs = $('#node-input-showInput').is(':checked') ? 1 : 0
      }

      //If checkbox is checked -> we need input
      $('#node-input-showInput').change(function () {
        checkIfInput()
      })

    }
  });
</script>



<!-- Properties -->
<script type="text/html" data-template-name="ads-client-connection-status">
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" placeholder="Name (optional)">
  </div>

  <div class="form-row">
      <label for="node-input-connection"><i class="fa fa-bookmark"></i> ADS connection</label>
      <input type="text" id="node-input-connection" placeholder="ADS Connection">
  </div>

  <div class="form-row">
    <input type="checkbox" id="node-input-showInput" style="display: inline-block; width: auto; vertical-align: top">
    <label for="node-input-showInput" style="width: 70%"><span>Show input for manual triggering</span></label>
  </div>

</script>



<!-- Help -->
<script type="text/html" data-help-name="ads-client-connection-status">
  <p>
    Reports status changes of the selected ADS connection.
  </p>


<h3>Properties (settings)</h3>
  <dl class="message-properties">
    <dt>Name
      <span class="property-type">string</span>
    </dt>
    <dd> Optional node name</dd>
  </dl>

  <dl class="message-properties">
    <dt>ADS connection
      <span class="property-type">ads-client-connection</span>
    </dt>
    <dd> ADS client connection (target system) to use</dd>
  </dl>

  <dl class="message-properties">
    <dt>Show input for manual triggering
      <span class="property-type">boolean</span>
    </dt>
    <dd> If checked, the status can be also read using input trigger</dd>
  </dl>



<h3>Outputs</h3>
  <dl class="message-properties">
    <dt>payload <span class="property-type">boolean</span></dt>
    <dd>ADS connection status as boolean (true = connection is OK) </dd>
    <dt>connection <span class="property-type">object</span></dt>
    <dd>Connection information (ads-client object)</dd>
    <dt>... <span class="property-type">any</span></dt>
    <dd>All input <code>msg</code> properties are forwarded (if using input)</dd>
  </dl>

<h3>Details</h3>
  <p>
    Status changes are automatically reported when detected.
  </p>
  <p>
    If <code>Show input for manual triggering</code> is checked, the status can also be read manually.
  </p>
  <p>
    <b>Note:</b> Status changes are always reported when detected (even if <code>Show input for manual triggering</code> is checked).
  </p>
  <p>
    The output <code>connection</code> has ads-client connection information, similar to:
    <pre>
connection {
  connected: true,
  isLocal: true,
  localAmsNetId: '192.168.5.131.1.1',
  localAdsPort: 38971,
  targetAmsNetId: '127.0.0.1.1.1',
  targetAdsPort: '851'
}</pre>
  </p>

<h3>References</h3>
  <ul>
    <li><a href="https://github.com/jisotalo/ads-client" target="_blank" style="text-decoration: underline">
      ads-client readme
    </a></li>
  </ul>
</script>